<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.faf.bean.Food"%>
<%
	String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"
		+ "UserGlobal" + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>饭否-查询</title>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/reset.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/main.css">
<script type="text/javascript"
	src="<%=basePath%>js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/jquery-ui.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/slide.js"></script>
</head>
<%
	Object pre_global_info_user_name = request.getSession().getAttribute("account");
String global_info_user_name;
HashMap<Integer, Integer> global_info_cart_id2count = null;
int i_food_count = 0;

if (null != pre_global_info_user_name) {
	global_info_user_name = pre_global_info_user_name.toString();

	global_info_cart_id2count = (HashMap<Integer, Integer>) request.getSession().getAttribute("global_cart");
	for (int value : global_info_cart_id2count.values())
		i_food_count += value;

} else {
	global_info_user_name = "";
}

request.getSession().setAttribute("pre-page", "/FAFCode/List" + "?" + request.getQueryString());

// load data
ArrayList<Food> foods = (ArrayList<Food>) request.getAttribute("list_dat_food");
%>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到饭否!</div>
			<div class="fr">
				<div class="login_info fl">
					欢迎您：<em><%=null == pre_global_info_user_name ? global_info_user_name : "请登录"%></em>
				</div>
				<%
					if (null == pre_global_info_user_name) {
				%>
				<div class="login_btn fl">
					<a href="/FAFCode/UserManagement/login.jsp">登录</a> <span>|</span> <a
						href="/FAFCode/UserManagement/register.jsp">注册</a>
				</div>
				<%
					} else {
				%>
				<div class="user_link fl">
					<a href="/FAFCode/UserManagement/user_center_info.jsp">用户中心</a> <span>|</span>
					<a href="/FAFCode/MyCart">我的购物车</a> <span>|</span> <a
						href="/FAFCode/UserManagement/user_center_order.jsp">我的订单</a>
				</div>
				<%
					}
				%>
			</div>
		</div>
	</div>

	<div class="search_bar clearfix">
		<a href="Index" class="logo fl"><img
			src="/FAFCode/Image/logo/logo.png"></a>
		<form action="List">
			<div class="search_con fl">
				<input type="text" class="input_text fl" name="serchContect"
					placeholder="搜索商品"> <input type="submit"
					class="input_btn fr" name="" value="搜索">
			</div>
		</form>
		<div class="guest_cart fr">
			<a href="/FAFCode/MyCart" class="cart_name fl">我的购物车</a>
			<div class="goods_count fl" id="show_count"><%=i_food_count%></div>
		</div>
	</div>

	<div class="navbar_con">
		<div class="navbar clearfix">
			<div class="subnav_con fl">
				<h1>全部菜品分类</h1>
				<span></span>
				<ul class="subnav">
					<li><a href="List?flag=1" class="fruit">炒菜</a></li>
					<li><a href="List?flag=2" class="seafood">主食</a></li>
					<li><a href="List?flag=3" class="meet">凉菜</a></li>
					<li><a href="List?flag=4" class="egg">饮品</a></li>
					<li><a href="List?flag=5" class="vegetables">速食</a></li>
					<li><a href="List?flag=6" class="ice">水果</a></li>
				</ul>
			</div>
			<ul class="navlist fl">
				<li><a href="Index">首页</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=1">炒菜</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=2">主食</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=3">凉菜</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=4">饮品</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=5">速食</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=6">水果</a></li>
			</ul>
		</div>
	</div>

	<div class="main_wrap clearfix">
		<div class="l_wrap fl clearfix">
			<div class="new_goods">
				<h3>热门菜品</h3>
				<ul>
					<%
						ArrayList<Food> foodme = (ArrayList<Food>) request.getAttribute("mefood");
					for (Food food : foodme) {
					%>
					<li><a href="Detail?fid=<%=food.getFoodId()%>"><img
							src="<%="/FAFCode/" + food.getFoodPhoto()%>"></a>
						<h4>
							<a href="Detail?fid=<%=food.getFoodId()%>"><%=food.getFoodName()%></a>
						</h4>
						<div class="prize">
							￥<%=food.getFoodPrice()%></div></li>
					<%
						}
					%>
				</ul>
			</div>
		</div>

		<div class="r_wrap fr clearfix">
			<div class="sort_bar">
				<a href="javascript:void(0)" id="default" class="active">默认</a> <a
					href="javascript:void(0)" id="foodPrice"
					onclick="price(<%=request.getAttribute("strFlag")%>,1)">价格</a>
				<a href="javascript:void(0)" id="count"
					onclick="count(<%=request.getAttribute("strFlag")%>,1)">人气</a>

			</div>
			<ul class="goods_type_list clearfix" id="food">

			</ul>

			<div class="pagenation" id="page"></div>
			<ul class="goods_type_list clearfix" id="good">

				<%
					if (null != foods) {
					for (Food food : foods) {
				%>

				<li><a href="Detail?fid=<%=food.getFoodId()%>"><img
						src="<%="/FAFCode/" + food.getFoodPhoto()%>"></a>
					<h4>
						<a href="Detail?fid=<%=food.getFoodId()%>"><%=food.getFoodName()%></a>
					</h4>
					<div class="operate">
						<span class="prize">￥<%=food.getFoodPrice()%></span> <span
							class="unit"><%=food.getFoodPrice()%> / 1份</span> <a
							href=<%="/FAFCode/AddCart?id=" + String.valueOf(food.getFoodId()) + "&num=1"%>
							class="add_goods" title="加入购物车"></a>
					</div></li>

				<%
					}
				}
				%>

			</ul>

			<%
				int iCountPage = Integer.parseInt(request.getAttribute("page-count").toString());
			int iCurrentPage = Integer.parseInt(request.getAttribute("page").toString());
			String strFlag = request.getParameter("flag");
			String strSearch = request.getParameter("serchContect");
			%>
			<div class="pagenation" id="goodpage">
				<%
					if (iCurrentPage > 1) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage - 1)%>>上一页</a>
				<%
					}
				%>

				<!--  -->
				<%
					if (iCountPage - iCurrentPage <= 0 && iCurrentPage - 4 > 0) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage - 4)%>><%=iCurrentPage - 4%></a>
				<%
					}
				%>

				<%
					if (iCountPage - iCurrentPage <= 1 && iCurrentPage - 3 > 0) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage - 3)%>><%=iCurrentPage - 3%></a>
				<%
					}
				%>
				<!--  -->

				<%
					if (iCurrentPage - 2 > 0) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage - 2)%>><%=iCurrentPage - 2%></a>
				<%
					}
				%>

				<%
					if (iCurrentPage - 1 > 0) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage - 1)%>><%=iCurrentPage - 1%></a>
				<%
					}
				%>

				<a href="#" class="active"><%=iCurrentPage%></a>

				<%
					if (iCountPage - iCurrentPage > 0) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage + 1)%>><%=iCurrentPage + 1%></a>
				<%
					}
				%>

				<%
					if (iCountPage - iCurrentPage > 1) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage + 2)%>><%=iCurrentPage + 2%></a>
				<%
					}
				%>

				<!--  -->
				<%
					if (iCurrentPage - 1 <= 0 && iCountPage - iCurrentPage > 2) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage + 3)%>><%=iCurrentPage + 3%></a>
				<%
					}
				%>

				<%
					if (iCurrentPage - 2 <= 0 && iCountPage - iCurrentPage > 3) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage + 4)%>><%=iCurrentPage + 4%></a>
				<%
					}
				%>
				<!--  -->

				<%
					if (iCountPage != iCurrentPage) {
				%>
				<a
					href=<%="/FAFCode/List" + "?" + (null == strFlag ? "serchContect=" + strSearch : "flag=" + strFlag) + "&page="
		+ (iCurrentPage + 1)%>>下一页</a>
				<%
					}
				%>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#default").click(function() {
				$("#food").empty();
				$("#page").empty();
				$("#good").show();
				$("#goodpage").show();
				$("#foodPrice").removeClass("active");
				$("#count").removeClass("active");
				$("#default").addClass("active");
			});
			$("#foodPrice").click(function() {
				$("#good").hide();
				$("#goodpage").hide();
				$("#default").removeClass("active");
				$("#count").removeClass("active");
				$("#foodPrice").addClass("active");
			});
			$("#count").click(function() {
				$("#good").hide();
				$("#goodpage").hide();
				$("#default").removeClass("active");
				$("#foodPrice").removeClass("active");
				$("#count").addClass("active");
			});
		});
		function price(foodType, currentPage) {
			var foodType = foodType;
			var currentPage = currentPage;
			$("#food").empty();
			$("#page").empty();
			$
					.ajax({
						type : 'post',
						data : {
							"foodType" : foodType,
							"currentPage" : currentPage
						},
						dataType : "json",
						url : "../FAFCode/FoodPriceSort",
						success : function(res) {
							$
									.each(
											res.page.resultList,
											function(index, obj) {
												$("#food")
														.append(
																"<li><a href='Detail?fid="
																		+ obj.foodId
																		+ "'><img src="
																		+ obj.foodPhoto
																		+ "></a><h4><a href='Detail?fid="
																		+ obj.foodId
																		+ "'>"
																		+ obj.foodName
																		+ "</a></h4><div class='operate'><span class='prize'>￥"
																		+ obj.foodPrice
																		+ "</span><span class='unit'>"
																		+ obj.foodPrice
																		+ "/ 1份</span> <a href='/FAFCode/AddCart?id="
																		+ obj.foodId
																		+ "&num=1 class='add_goods' title='加入购物车'></a></div></li>");
											})

							$("#page")
									.append(
											"	<a href='#' onclick='price("
													+ foodType
													+ ","
													+ res.page.firstPage
													+ ")'>首页</a><a href='#' onclick='price("
													+ foodType
													+ ","
													+ res.page.prePage
													+ ")'>上一页</a><a href='#' onclick='price("
													+ foodType
													+ ","
													+ res.page.nextPage
													+ ")'>下一页</a><a href='#' onclick='price("
													+ foodType + ","
													+ res.page.lastPage
													+ ")'>尾页</a>")
						},
						error : function() {
							alert("发生错误");
						}
					})
		}
		
		function count(foodType, currentPage) {
			var foodType = foodType;
			var currentPage = currentPage;
			$("#food").empty();
			$("#page").empty();
			$
					.ajax({
						type : 'post',
						data : {
							"foodType" : foodType,
							"currentPage" : currentPage
						},
						dataType : "json",
						url : "../FAFCode/FoodCountSort",
						success : function(res) {
							$
									.each(
											res.page.resultList,
											function(index, obj) {
												$("#food")
														.append(
																"<li><a href='Detail?fid="
																		+ obj.foodId
																		+ "'><img src="
																		+ obj.foodPhoto
																		+ "></a><h4><a href='Detail?fid="
																		+ obj.foodId
																		+ "'>"
																		+ obj.foodName
																		+ "</a></h4><div class='operate'><span class='prize'>￥"
																		+ obj.foodPrice
																		+ "</span><span class='unit'>"
																		+ obj.foodPrice
																		+ "/ 1份</span> <a href='/FAFCode/AddCart?id="
																		+ obj.foodId
																		+ "&num=1 class='add_goods' title='加入购物车'></a></div></li>");
											})

							$("#page")
									.append(
											"	<a href='#' onclick='count("
													+ foodType
													+ ","
													+ res.page.firstPage
													+ ")'>首页</a><a href='#' onclick='count("
													+ foodType
													+ ","
													+ res.page.prePage
													+ ")'>上一页</a><a href='#' onclick='count("
													+ foodType
													+ ","
													+ res.page.nextPage
													+ ")'>下一页</a><a href='#' onclick='count("
													+ foodType + ","
													+ res.page.lastPage
													+ ")'>尾页</a>")
						},
						error : function() {
							alert("发生错误");
						}
					})
		}
	</script>
</body>
</html>